package com.irdstudio.tdp.executor.core.dao;

import com.irdstudio.tdp.executor.core.dao.domain.MigrateDictInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 新系统字典信息类-[表名: 新系统字典信息(migrate_dict_info)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateDictInfoDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateDictInfoDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateDictInfo(MigrateDictInfo migrateDictInfo) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_dict_info ( "
                + "dict_id,subs_code,dict_code,dict_name,dict_is_std,dict_remarks"
                + ") values ("+ "?,?,?,?,?,?" +")"
                );
            ps.setObject(1, migrateDictInfo.getDictId());
            ps.setObject(2, migrateDictInfo.getSubsCode());
            ps.setObject(3, migrateDictInfo.getDictCode());
            ps.setObject(4, migrateDictInfo.getDictName());
            ps.setObject(5, migrateDictInfo.getDictIsStd());
            ps.setObject(6, migrateDictInfo.getDictRemarks());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateDictInfo is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateDictInfo migrateDictInfo) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_dict_info where 1=1"
                + " AND dict_id = ? "
                );
            ps.setObject(1, migrateDictInfo.getDictId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateDictInfo is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateDictInfo migrateDictInfo) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_dict_info set "
                + " dict_id = ? ,"
                + " subs_code = ? ,"
                + " dict_code = ? ,"
                + " dict_name = ? ,"
                + " dict_is_std = ? ,"
                + " dict_remarks = ? "
                + " where 1=1"
                + " AND dict_id = ? "
                );
            ps.setObject(1, migrateDictInfo.getDictId());
            ps.setObject(2, migrateDictInfo.getSubsCode());
            ps.setObject(3, migrateDictInfo.getDictCode());
            ps.setObject(4, migrateDictInfo.getDictName());
            ps.setObject(5, migrateDictInfo.getDictIsStd());
            ps.setObject(6, migrateDictInfo.getDictRemarks());
            ps.setObject(7, migrateDictInfo.getDictId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateDictInfo is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateDictInfo queryByPk(MigrateDictInfo migrateDictInfo) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateDictInfo result = null;
        try {
            ps = conn.prepareStatement("select dict_id,subs_code,dict_code,dict_name,dict_is_std,dict_remarksfrom migrate_dict_info where 1=1 "
                + " AND dict_id = ? "
                );
            ps.setObject(1, migrateDictInfo.getDictId());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateDictInfo();
				result.setDictId(rs.getString("dict_id"));
				result.setSubsCode(rs.getString("subs_code"));
				result.setDictCode(rs.getString("dict_code"));
				result.setDictName(rs.getString("dict_name"));
				result.setDictIsStd(rs.getString("dict_is_std"));
				result.setDictRemarks(rs.getString("dict_remarks"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateDictInfo is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateDictInfo 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateDictInfo> queryAll(MigrateDictInfo migrateDictInfo) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateDictInfo> result = new ArrayList<>();
        try {
            String sql = "select dict_id,subs_code,dict_code,dict_name,dict_is_std,dict_remarksfrom migrate_dict_info where 1=1 ";
                if (migrateDictInfo.getDictId() != null) {
                    sql = sql + " AND dict_id =  '" + migrateDictInfo.getDictId() + "'";
                }
                if (migrateDictInfo.getSubsCode() != null) {
                    sql = sql + " AND subs_code =  '" + migrateDictInfo.getSubsCode() + "'";
                }
                if (migrateDictInfo.getDictCode() != null) {
                    sql = sql + " AND dict_code =  '" + migrateDictInfo.getDictCode() + "'";
                }
                if (migrateDictInfo.getDictName() != null) {
                    sql = sql + " AND dict_name =  '" + migrateDictInfo.getDictName() + "'";
                }
                if (migrateDictInfo.getDictIsStd() != null) {
                    sql = sql + " AND dict_is_std =  '" + migrateDictInfo.getDictIsStd() + "'";
                }
                if (migrateDictInfo.getDictRemarks() != null) {
                    sql = sql + " AND dict_remarks =  '" + migrateDictInfo.getDictRemarks() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateDictInfo obj = new MigrateDictInfo();
				obj.setDictId(rs.getString("dict_id"));
				obj.setSubsCode(rs.getString("subs_code"));
				obj.setDictCode(rs.getString("dict_code"));
				obj.setDictName(rs.getString("dict_name"));
				obj.setDictIsStd(rs.getString("dict_is_std"));
				obj.setDictRemarks(rs.getString("dict_remarks"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateDictInfo is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
